<%@ Page PathInChannel="ajax/lesson/text{Ext}" RunAt="Execute" %>
<ali:List ID="Member" Mark="Course" Object="Member" Page="1" PageSize="1" Condition="IsArchived=false and IsCanceled=false and IsDelivered=2 and (ExpiredTime IS NULL or ExpiredTime>DateTime.Now) and UserID=User['ID']" />
<ali:List ID="CourseStudy" Mark="Course" Object="CourseStudy" Page="1" PageSize="1" Condition="CourseID=Request.QueryString['CourseID'] and UserID=User['ID']" />
<ali:List ID="CourseOrder" Mark="Course" Object="Order" Page="1" PageSize="1" Condition="IsArchived=false and IsCanceled=false and IsDelivered=2 and (ExpiredTime IS NULL or ExpiredTime>DateTime.Now) and UserID=User['ID'] and CourseID=Request.QueryString['CourseID']"/>
{
	"lessons":
	[
		<ali:List Mark="Course" Object="Lesson" Condition="CourseID=Request.QueryString['CourseID']">
			<ali:Record>
				{"ID":<%=InnerID%>,"Guid":"<%=ID%>","Name":"<%=Name%>","IsChapter":<%=Convert.ToString(IsChapter).ToLower()%>,"isFree":<%=FreeDuration%>}
				<ali:Separator>,</ali:Separator>
			</ali:Record>
		</ali:List>
	]
	<%If CourseOrder[Count] > 0 || Convert.ToBoolean(Request.QueryString["IsMemberFree"]) && Member[Count] > 0%>
		,"hasBuy":true
		,"courseStudyId":<%If CourseStudy[Count] > 0 %><%=CourseStudy[InnerID]%><%Else%><%=AddCourseStudy()%><%EndIf%>
	<%Else%>
		,"hasBuy":false
	<%EndIf%>
}
<script language="c#" runat="server">
protected int AddCourseStudy()
{
	int instanceId = Convert.ToInt32(Request.QueryString["InstanceID"]);
	using (AliCMS.CourseModule.ICourseStudy bll = (AliCMS.CourseModule.ICourseStudy)Manager.CreateObject("CourseStudy", instanceId, PageLang, false))
	{
		Dictionary<string, object> item = new Dictionary<string, object>();
		item[AliCMS.CourseModule.Alias.CourseStudy.UserID] = new Guid(Manager.User["ID"]);
		item[AliCMS.CourseModule.Alias.CourseStudy.CourseID] = new Guid(Request.QueryString["CourseID"]);
		item[AliCMS.CourseModule.Alias.CourseStudy.CourseName] = Request.QueryString["CourseName"];
		item[AliCMS.CourseModule.Alias.CourseStudy.CourseThumbnail] = Request.QueryString["CourseThumbnail"];
		item[AliCMS.CourseModule.Alias.CourseStudy.SubInstanceID] = Request.QueryString["SubInstanceID"];
		Guid guid = bll.Add(item);
		
		return bll.GetInnerIDFromGuid(guid);
	}
}
</script>